# ProgressBar - Implements progress bar widget.
# Copyright (C) 2007 Lauri Taimila
# 
# Entertainer is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Entertainer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import math
import cairo
import clutter
from clutter.cluttercairo import CairoTexture

class ProgressBar(clutter.Group):
    """
    Progress bar widget.
    
    This widget implements simple progress bar.
    """
    
    def __init__(self, width, height, color):
        """
        Initialize progress bar with 0% completed
        @param width: Width of the widget
        @param height: Height of the widget
        @param color: clutter.Color - Color of the progress bar
        """
        clutter.Group.__init__(self)
        self.width = width
        self.height = height
        self.progress = 0
        self.color = self._clutter_color_to_cairo_color(color)
        self.background = CairoTexture(width, height)
        self.foreground = CairoTexture(height, height)
        self.foreground.set_anchor_point_from_gravity(clutter.GRAVITY_CENTER)
        self._draw_background()
        self._draw_foreground()
        self.background.show()
        self.foreground.show()
        self.add(self.background)
        self.add(self.foreground)
    
    def _draw_background(self):
        """
        Draw background graphics
        @param context: Cairo context
        """
        context = self.background.cairo_create()
        context.set_line_width(self.height * 0.09)
        context.set_source_rgba(self.color[0], self.color[1], self.color[2], 0.7)
        
        context.arc(self.height / 2, 
                    self.height / 2,
                    self.height / 2 -2,
                    math.pi / 2,
                    math.pi * 1.5)       
        context.arc(self.width - self.height / 2, 
                    self.height / 2,
                    self.height / 2 -2,
                    math.pi * 1.5,
                    math.pi / 2)
        context.close_path()
        
        context.stroke()
        del context
        
    def _draw_foreground(self):
        """
        Draw foreground graphics
        @param context: Cairo context
        """
        context = self.foreground.cairo_create()
        context.scale(self.height, self.height)
        context.set_source_rgba(self.color[0], self.color[1], self.color[2], 1.0)
        context.arc(0.5, 0.5, 0.3, 0, 2 * math.pi)
        context.fill()
        del context
    
    def _clutter_color_to_cairo_color(self, color):
        """
        Transform from 0-255 to 0-1.
        @param color: clutter.Color
        """
        r = float(color.red) / 255.0
        g = float(color.green) / 255.0
        b = float(color.blue) / 255.0
        a = float(color.alpha) / 255.0
        return r,g,b,a
    
    def set_progress(self, progress):
        """
        Set current position of the progress bar.
        @param progress: Integer between 0-100.
        """
        self.progress = progress
        self.foreground.set_position(int((self.width - self.height) * progress), 0)
        
    def get_progress(self):
        """
        Get current level of progress.
        @return: Integer between 0-100
        """
        return self.progress
        